﻿#include "AppMainWindow.h"
#include <QProcess>
#include <QApplication>
#include <QDebug>
#include <QLocale>
#include "DAAppCore.h"
#include "DAMessageHandler.h"
#include "DAPybind11InQt.h"
#include "DAPyScripts.h"
#include <QFileInfo>
#include <QDir>
#include <QObject>
#include "DAPyInterpreter.h"
#include "DAPybind11QtTypeCast.h"
bool initializePythonEnv(const QString& appabsPath);
QString getPythonInterpreterPath(const QString& appabsPath);
QString getPythonScriptsPath(const QString& appabsPath);
void setAppFont();
int main(int argc, char* argv[])
{
    //注册旋转文件消息捕获
    DA::daRegisterRotatingMessageHandler("./log/da_log.log");
    for (int i = 0; i < argc; ++i) {
        qDebug() << argv[ i ];
    }
    QApplication app(argc, argv);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    //注册元对象
    DA::PY::registerMetaType();
    setAppFont();
    DA::DAAppCore& core = DA::DAAppCore::getInstance();
    //初始化python环境
    if (initializePythonEnv(argv[ 0 ])) {
        core.setPythonInterpreterInitializedMode(true);
    } else {
        core.setPythonInterpreterInitializedMode(false);
    }

    // TODO 此处进行一些核心的初始化操作
    DA::AppMainWindow w;

    w.show();
    int r = app.exec();
    DA::daUnregisterMessageHandler();
    return r;
}

void setAppFont()
{
    QFont font = QApplication::font();
    font.setFamily(QStringLiteral(u"微软雅黑"));
    QApplication::setFont(font);
}
/**
 * @brief 获取Python环境路径
 * @param appabsPath app的绝对路径，如F:\src\data-work-flow\data-work-flow\bin_qt5.14.2_mingw_debug\daWork.exe
 * @return
 */
QString getPythonInterpreterPath(const QString& appabsPath)
{
    QFileInfo fi(appabsPath);
    return fi.absoluteDir().absolutePath() + QDir::separator() + "Python";
}

/**
 * @brief 获取python脚本的位置
 * @param appabsPath
 * @return
 */
QString getPythonScriptsPath(const QString& appabsPath)
{
    QFileInfo fi(appabsPath);
    return fi.absoluteDir().absolutePath() + QDir::separator() + "PyScripts";
}

/**
 * @brief 初始化python环境
 * @param appabsPath
 * @return
 */
bool initializePythonEnv(const QString& appabsPath)
{
    try {
        DA::DAPyInterpreter& python = DA::DAPyInterpreter::getInstance();
        do {
            QString pypath = getPythonInterpreterPath(appabsPath);
            qInfo() << QObject::tr("Python interpreter path is %1").arg(pypath);
            python.setPythonHomePath(pypath);
        } while (0);
        python.initializePythonInterpreter();
        //初始化环境成功后，加入脚本路径
        do {
            DA::DAPyScripts& scripts = DA::DAPyScripts::getInstance();
            QString scriptPath       = getPythonScriptsPath(appabsPath);
            qInfo() << QObject::tr("Python scripts path is %1").arg(scriptPath);
            scripts.appendSysPath(scriptPath);
            if (!scripts.initScripts()) {
                qCritical() << QObject::tr("Scripts initialize error");
                return false;
            }
        } while (0);
    } catch (const std::exception& e) {
        qCritical() << QObject::tr("Initialize python environment error:%1").arg(e.what());
        //这时告诉内核Python没有初始化成功
        return false;
    }
    return true;
}
